﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Net.Mail;

public partial class Interviewer_ChangeStatusInterView : System.Web.UI.Page
{
    InterviewMethord IM = new InterviewMethord();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            int InterviewID = Convert.ToInt32(Request.QueryString["ID"].ToString());
            if (!IsPostBack)
            {
                LoadInformationInterviewID(InterviewID);
            }
            btnSave.Enabled = false;
        }
        catch (Exception ex)
        {
        }

    }

    private void LoadInformationInterviewID(int _InterviewID)
    {
        DataTable tb = IM.ReturnInfomationInterview(_InterviewID);
        if (tb.Rows.Count > 0)
        {
            SqlDataReader dr = IM.ReturnInfomationInterviews(tb.Rows[0]["InterviewID"].ToString());
            IM.connClose();
            txtInterviewID.Text = tb.Rows[0]["InterviewID"].ToString();
            txtInterviewerID.Text = tb.Rows[0]["InterviewerID"].ToString();
            txtVacancyID.Text = tb.Rows[0]["VacancyID"].ToString();
            txtApplicantID.Text = tb.Rows[0]["ApplicantID"].ToString();
            txtDateInterview.Text = tb.Rows[0]["DateInterview"].ToString().Substring(0, tb.Rows[0]["DateInterview"].ToString().IndexOf(" "));
            txtTimeDetails.Text = tb.Rows[0]["TimeDetails"].ToString();

            switch (Convert.ToInt32(tb.Rows[0]["Status"].ToString()))
            {
                case 1:
                    ddlStatus.SelectedIndex = 0;
                    break;
                case 2:
                    ddlStatus.SelectedIndex = 1;
                    break;
                case 3:
                    ddlStatus.SelectedIndex = 2;
                    break;
                   
            }
            
        }
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        lblSuccess.Text = "";
        lblError.Text = "";
        bool check = IM.UpdateStatusInterview(Convert.ToInt32(txtInterviewID.Text), Convert.ToInt32(ddlStatus.SelectedItem.Value));
        if (check)
        {
            if (ddlStatus.SelectedItem.ToString().Equals("Accept"))
            {
                try
                {
                    MailMessage msg = new MailMessage();
                    msg.From = new MailAddress("eprojectgroup1sem3@gmail.com");
                    msg.To.Add(IM.ReturnEmail(txtApplicantID.Text));
                    msg.Subject = "Notice of the time of interview ABC company";
                    msg.Body = "  Hi! Application for your consideration we has been inspected. Appointment you right " + txtTimeDetails.Text + " on " + txtDateInterview.Text + " to the company's headquarters to participate in interviews appointment.";
                    SmtpClient sc = new SmtpClient("smtp.gmail.com");
                    sc.Port = 25;
                    sc.Credentials = new NetworkCredential("eprojectgroup1sem3@gmail.com", "admin!@#$%^");
                    sc.EnableSsl = true;
                    sc.Send(msg);
                    lblSuccess.Text = "Update success and email interview appointment has been sented";
                }
                catch (Exception ex)
                {

                }
            }
            else if (ddlStatus.SelectedItem.ToString().Equals("Waiting"))
            {
                lblSuccess.Text = "Changed the status of InterviewID to " + ddlStatus.SelectedItem.Text;
            }
            else
            {
                Response.Write("<script type='text/javascript'>alert('You need proposed new interview.'); window.location = 'InterviewerProposedInterview.aspx?ID="+txtInterviewID.Text+"';</script>");
            }
            
        }
        else
        {
            lblError.Text = "Changes process failure status";
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("InterviewViewInterviewInfor.aspx");
    }
    protected void btnProposed_Click(object sender, EventArgs e)
    {
    }
}